<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;
use Think\Controller;
use Think\Upload;

/**
 * Description of CommonController
 *
 * @author Rung-PC
 */
class CommonController extends Controller{
    protected $no_login_act = ['index','login','captcha','check','logout'];

    public function __construct() {
        parent::__construct();
        if(!session('?user_id') && !session('?user_name') && !in_array(ACTION_NAME,$this->no_login_act)){
               echo "<script>top.location='".U('index/index')."'</script>";
               die;
        }
    }

        public function upload($type,$path,$subName,$maxSize=259715200,$rootPath='./Uploads/'){
         $config=array(
                'exts'=>$type,
                'savePath'=>$path,
                'subName'=>$subName,
                'maxSize'=>$maxSize,
                'rootPath'=>$rootPath,
            );
            $upload = new Upload($config);
            $info=$upload->upload();
            if (!$info) {
                $this->error($upload->getError());
            } 
           return $info; 
    }
    
    public function create_dirs($path){  
            if (!is_dir($path)){  
                $directory_path = "";  
                $directories = explode("/",$path);  
                array_pop($directories);  

                foreach($directories as $directory){  
                    $directory_path .= $directory."/";  
                    if (!is_dir($directory_path)){  
                        mkdir($directory_path);  
                        chmod($directory_path, 0777);  
                    }  
                }  
            }  
    } 
    
     public function delDirAndFile( $dirName ){  
        if ( $handle = opendir( "$dirName" ) ) {  
           while ( false !== ( $item = readdir( $handle ) ) ){  
                if ( $item != "." && $item != ".." ) {  
                    if ( is_dir( "$dirName/$item" ) ) {  
                         $this->delDirAndFile( "$dirName/$item" );  
                    } else {  
                         unlink( "$dirName/$item" ) ;  
                    }  
                }  
           }  
           closedir( $handle );  
           rmdir( $dirName );  
        }  
    } 
    
    public function guid(){
            if (function_exists('com_create_guid')){
                return com_create_guid();
            }else{
                mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
                $charid = strtoupper(md5(uniqid(rand(), true)));
                $hyphen = chr(45);// "-"
                $uuid = chr(123)// "{"
                        .substr($charid, 0, 8).$hyphen
                        .substr($charid, 8, 4).$hyphen
                        .substr($charid,12, 4).$hyphen
                        .substr($charid,16, 4).$hyphen
                        .substr($charid,20,12)
                        .chr(125);// "}"
                return $uuid;
            }
    }
    
    public function get_order_sn(){
            /* 选择一个随机的方案 */
            mt_srand((double) microtime() * 1000000);

            return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }
}
